<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/1
 * Time: 15:33
 */
namespace App\Services;
use App\Enum\MediaLikeConfigEnum;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\MediaLikeConfig;
use App\Models\Media;
use App\Models\User;
use App\Enum\MediaEnum;

class MediaLikeConfigService
{
    private static $_msg = ['code' => 0, 'msg' => ''];

    /**
     * @desc 创建视频点赞配置
     * @param $params
     * @return mixed
     */
    public static function add_media_like_config($params)
    {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $data = self::_handle_data($params);
        $data['created_at'] = $data['updated_at'] = time2date();
        if(!MediaLikeConfig::insert($data)) {
            self::_set_msg('10000', '写入视频配置失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 修改视频点赞配置
     * @param $params
     * @return mixed
     */
    public static function modify_media_like_config($params) {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $where = ['id' => $params['id']];
        $data = self::_handle_data($params);
        $data['updated_at'] = time2date();
        if(!MediaLikeConfig::where($where)->update($data)) {
            self::_set_msg('10000', '修改视频配置失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 修改视频点赞配置状态
     * @param $params
     * @return array
     */
    public static function modify_media_like_config_status($params)
    {
        $is_record = MediaLikeConfig::where(['id' => $params['id']])->first();
        if(!$is_record) {
            self::_set_msg('10000', '视频点赞配置数据异常');
            return self::$_msg;
        }

        if($is_record->status == 3) {
            self::_set_msg('10000', '视频点赞配置已结束');
            return self::$_msg;
        }

        if(!in_array($params['status'], MediaLikeConfigEnum::$media_like_status_arr)) {
            self::_set_msg('10000', '视频点赞配置状态异常');
            return self::$_msg;
        }

        $data = ['operator_id' => $params['operator_id'], 'status' => $params['status'], 'updated_at' => time2date()];
        if(!MediaLikeConfig::where(['id' => $params['id']])->update($data)) {
            self::_set_msg('10000', '修改失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 检测点赞配置数据
     * @param $params
     * @return bool
     */
    private static function _check_data($params)
    {
        if(mb_strlen($params['config_name']) > 20) {
            self::_set_msg('10000', '视频点赞配置名称不能超过20个字符');
            return false;
        }
        // 检测点赞对象
        if(!in_array($params['media_type'], MediaLikeConfigEnum::$media_list_type_arr)) {
            self::_set_msg('10000', '视频点赞对象异常');
            return false;
        }

        if($params['media_type'] == MediaLikeConfigEnum::MEDIA_LIKE_TYPE_USER) {
            if(empty($params['uid'])) {
                self::_set_msg('10000', '用户的微影号不能为空');
                return false;
            }
            $user_info = User::where(['id' => (int)$params['uid'], 'status' => 1])->first();
            if(empty($user_info)) {
                self::_set_msg('10000', '用户的微影号异常');
                return false;
            }
        } elseif ($params['media_type'] == MediaLikeConfigEnum::MEDIA_LIKE_TYPE_INDIVIDUAL) {
            if(empty($params['media_id'])) {
                self::_set_msg('10000', '视频ID不能为空');
                return false;
            }

            $media_ids = explode(',', $params['media_id']);
            if(count($media_ids)  > 3) {
                self::_set_msg('10000', '视频ID不能超过3个');
                return false;
            }
            foreach ($media_ids as $media_id) {
                $media_info = Media::where(['id' => $media_id])->first();
                if(empty($media_info)) {
                    self::_set_msg('10000', '视频ID【'.$media_id.'】不存在');
                    return false;
                }

                if($media_info->is_delete == MediaEnum::MEDIA_DELETE_Y) {
                    self::_set_msg('10000', '视频ID【'.$media_id.'】已删除');
                    return false;
                }
                if($media_info->status != MediaEnum::MEDIA_STATUS_ENABLE) {
                    self::_set_msg('10000', '视频ID【'.$media_id.'】未审核');
                    return false;
                }
                if($media_info->is_private != MediaEnum::MEDIA_PRIVATE_OPEN) {
                    self::_set_msg('10000', '视频ID【'.$media_id.'】不是公开的视频');
                    return false;
                }
                if($media_info->is_forward == MediaEnum::MEDIA_FORWARD_Y) {
                    self::_set_msg('10000', '视频ID【'.$media_id.'】是转发视频');
                    return false;
                }
            }
        }

        // 检测点赞频率
        if(!is_int($params['frequency'])) {
            self::_set_msg('10000', '视频点赞频率异常');
            return false;
        }

        // 检测点赞的周期类型
        if(!in_array($params['cycle_type'], MediaLikeConfigEnum::$media_like_cycle_arr)) {
            self::_set_msg('10000', '视频点赞周期类型');
            return false;
        }

        // 检测周期时间
        if(!is_int($params['cycle_time'])) {
            self::_set_msg('10000', '视频点赞周期时间为整型');
            return false;
        }

        if(!$start_time = strtotime($params['start_time'])) {
            self::_set_msg('10000', '开始时间格式不正确');
            return false;
        }

        if(!$end_time = strtotime($params['end_time'])) {
            self::_set_msg('10000', '结束时间格式不正确');
            return false;
        }

        if($start_time > $end_time) {
            self::_set_msg('10000', '开始时间不能大于结束时间');
            return false;
        }

        // 检测视频配置状态
        if(!empty($params['id'])) {
            $is_record = MediaLikeConfig::where(['id' => $params['id']])->first();
            if(!$is_record) {
                self::_set_msg('10000', '视频点赞配置数据异常');
                return false;
            }

            if($is_record->status == 3) {
                self::_set_msg('10000', '视频点赞配置已结束');
                return false;
            }
        }

        if($params['account_num'] < 1 || $params['account_num'] > 50) {
            self::_set_msg('10000', '营销账号数量不符合');
            return false;
        }

        return true;
    }

    /**
     * @desc 整合数据
     * @param $params
     * @return array
     */
    private static function _handle_data($params)
    {
        return $data = [
            'config_name' => trim($params['config_name']),
            'uid' => $params['media_type'] == MediaLikeConfigEnum::MEDIA_LIKE_TYPE_USER ? $params['uid'] : 0,
            'media_id' => $params['media_type'] == MediaLikeConfigEnum::MEDIA_LIKE_TYPE_INDIVIDUAL ? $params['media_id'] : '',
            'media_type' => $params['media_type'],
            'frequency' => $params['frequency'],
            'cycle_type' => $params['cycle_type'],
            'cycle_time' => $params['cycle_time'],
            'start_time' => strtotime($params['start_time']),
            'end_time' => strtotime($params['end_time']),
            'operator_id' => $params['operator_id'],
            'account_num' => (int)$params['account_num'],
        ];
    }

    /**
     * @desc 设置提示信息
     * @param $code
     * @param $msg
     * @return array
     */
    private static function _set_msg($code, $msg)
    {
        return self::$_msg = ['code' => $code, 'msg' => $msg];
    }
}